/*
 * @Author: 麦冬果果
 * @Date: 2021-09-02 16:13:48
 * @LastEditTime: 2021-09-11 09:09:54
 * @LastEditors: 麦冬果果
 * @Description: 路由文件：所有应用的路由路径第一个名称为应用名称 例如/应用英文名称/*
 * @FilePath: /svc/src/boot/router.go
 * 可以输入预定的版权声明、个性签名、空行等
 */
package boot

import (
	"fastapi/src/app/api"
	"fastapi/src/app/hook"
	"fastapi/src/app/middlewares"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
)

var Router = route{}

type route struct{}

func (rt *route) Run() {
	domain := g.Cfg().GetString("server.Domain")
	s := g.Server()
	d := s.Domain(domain)
	group := d.Group("/")
	//上传的文件允许跨域请求
	group.Hook("/pub_upload/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
		r.Response.CORSDefault()
	})
	//这里是手动路由
	// group.ALL("/fastApi/index", api.Index)

	//fastapi注册
	init_fastApi(group)

	glog.Debug("[router][路由开启]")
}

/**
 * @description: 绑定fastapi路由
 * @param {*ghttp.RouterGroup} group
 * @return {*}
 * /appName 应用英文名称
 */
func init_fastApi(group *ghttp.RouterGroup) {
	//钩子
	group.Hook("/appName/{-gname}/{-ename}", ghttp.HookBeforeServe, hook.BeforeServer)
	group.Hook("/appName/{-gname}/{-ename}", ghttp.HookAfterServe, hook.AfterServer)
	group.Hook("/appName/{-gname}/{-ename}", ghttp.HookBeforeOutput, hook.BeforeOutput)
	group.Hook("/appName/{-gname}/{-ename}", ghttp.HookAfterOutput, hook.AfterOutput)
	//验证请求参数
	group.Middleware(middlewares.Parse)
	//路由
	group.POST("/appName/{-gname}/{-ename}", func(r *ghttp.Request) {
		api.FastApi.Index(r)
	})
}
